package com.ysm.smfu.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.ysm.smfu.BaseApp;
import com.ysm.smfu.R;
import com.ysm.smfu.activity.BaseActivity;
import com.zhy.autolayout.AutoLinearLayout;


public class UIUtils {

    public static final String intentKey = "className";
    public static final String intentData = "data";
    public static String intentCode = "code";

    /**
     * dip转换px
     */
    public static int dip2px(int dip) {
        final float scale = getContext().getResources().getDisplayMetrics().density;
        return (int) (dip * scale + 0.5f);
    }

    /**
     * pxz转换dip
     */

    public static int px2dip(int px) {
        final float scale = getContext().getResources().getDisplayMetrics().density;
        return (int) (px / scale + 0.5f);
    }

    /**
     * 根据资源id 获取字符串数组
     *
     * @param id
     * @return
     */
    public static String[] getStringArray(int id) {
        return getResources().getStringArray(id);
    }

    /**
     * 获取Resources对象
     *
     * @return
     */
    public static Resources getResources() {
        return getContext().getResources();
    }

    /**
     * 获取上下文对象
     *
     * @return
     */
    public static Context getContext() {
        return BaseApp.getInstance();
    }

    /**
     * xml --->View对象
     *
     * @param id
     * @return
     */
    public static View inflate(int id) {
        return View.inflate(UIUtils.getContext(), id, null);
    }

    /**
     * 获取dimens
     *
     * @param id
     * @return
     */
    public static int getDimens(int id) {
        return getResources().getDimensionPixelSize(id);
    }

    /**
     * 启动新的Activity，不传递数据
     *
     * @param clazz
     */
    public static void startActivity(Class clazz) {
        Intent intent = new Intent(UIUtils.getContext(), clazz);

        if (BaseActivity.activity == null) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//指定任务栈
            getContext().startActivity(intent);
        } else {
            //不需要指定任务栈
            BaseActivity.activity.startActivity(intent);

        }
    }
    /**
     * 根据Intent启动新的Activity
     *
     * @param intent
     */
    public static void startActivity(Intent intent) {
        if (BaseActivity.activity == null) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//指定任务栈
            getContext().startActivity(intent);
        } else {
            //不需要指定任务栈
            BaseActivity.activity.startActivity(intent);
        }

    }

    /**
     * 启动新的Activity，传递数据
     *
     * @param className
     */
    public static void startActivity(Class clazz, String key, String className) {
        Intent intent = new Intent(UIUtils.getContext(), clazz);
        intent.putExtra(key, className);
        if (BaseActivity.activity == null) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//指定任务栈
            getContext().startActivity(intent);
        } else {
            //不需要指定任务栈
            BaseActivity.activity.startActivity(intent);
        }
    }
    /**
     * 启动新的Activity，传递数据
     *
     * @param className
     */
    public static void startActivity(Class clazz, String key, boolean className) {
        Intent intent = new Intent(UIUtils.getContext(), clazz);
        intent.putExtra(key, className);
        if (BaseActivity.activity == null) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//指定任务栈
            getContext().startActivity(intent);
        } else {
            //不需要指定任务栈
            BaseActivity.activity.startActivity(intent);
        }
    }

    /**
     * 启动新的Activity，传递数据
     *
     * @param className
     */
    public static void startActivity(Class clazz, String key, int className) {
        Intent intent = new Intent(UIUtils.getContext(), clazz);
        intent.putExtra(key, className);
        if (BaseActivity.activity == null) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//指定任务栈
            getContext().startActivity(intent);
        } else {
            //不需要指定任务栈
            BaseActivity.activity.startActivity(intent);
        }
    }

    /**
     * id --- >string
     *
     * @param id
     * @return
     */
    public static String getString(int id) {
        return getResources().getString(id);
    }

    /**
     * id_---->Drawable
     *
     * @param id
     * @return
     */
    public static Drawable getDrawable(int id) {
        return getResources().getDrawable(id);
    }

    /**
     * 弹出键盘
     */
    public static void showKeyboard(View v) {
        ((InputMethodManager) getContext().getSystemService(getContext().INPUT_METHOD_SERVICE)).showSoftInput(v, 0);
    }

    /**
     * 打开键盘
     */
    public static void openKeyboard() {
        InputMethodManager imm = (InputMethodManager) getContext()

                .getSystemService(Context.INPUT_METHOD_SERVICE);

        imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);

    }

    /**
     * fragment中隐藏键盘,键盘的显示和隐藏于activity有直接关系，需要使用activity
     */
    public static void hideKeyboard(Fragment fragment) {
        ((InputMethodManager) fragment.getActivity().getSystemService(fragment.getActivity().INPUT_METHOD_SERVICE))
                .hideSoftInputFromWindow((fragment.getActivity().getCurrentFocus().getWindowToken()),
                        InputMethodManager.HIDE_NOT_ALWAYS);
    }

    /**
     * fragment中隐藏键盘,键盘的显示和隐藏于activity有直接关系，需要使用activity
     */
    public static void hideKeyboard(Activity activity) {
        ((InputMethodManager) activity.getSystemService(activity.INPUT_METHOD_SERVICE))
                .hideSoftInputFromWindow((activity.getCurrentFocus().getWindowToken()),
                        InputMethodManager.HIDE_NOT_ALWAYS);
    }

    /**
     * 限制小数点后个数
     *
     * @param num 小数点后位个数
     */
    public static void setDeciNum(final EditText editText, final int num) {
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.toString().contains(".")) {
                    if (s.length() - 1 - s.toString().indexOf(".") > num) {
                        s = s.toString().subSequence(0, s.toString().indexOf(".") + num + 1);
                        editText.setText(s);
                        editText.setSelection(s.length());
                    }
                }
                if (s.toString().trim().substring(0).equals(".")) {
                    s = "0" + s;
                    editText.setText(s);
                    editText.setSelection(2);
                }
                if (s.toString().startsWith("0")
                        && s.toString().trim().length() > 1) {
                    if (!s.toString().substring(1, 2).equals(".")) {
                        editText.setText(s.subSequence(0, 1));
                        editText.setSelection(1);
                        return;
                    }
                }
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });
    }

    //    构造指示点
    public static ImageView[] initIndicators(int count, AutoLinearLayout ovels,int unSelect,int select) {
        if (count < 2)
            return null;
        ImageView[] indicators = new ImageView[count]; // 定义指示器数组大小
        for (int i = 0; i < count; i++) {
            // 循环加入指示器
            indicators[i] = new ImageView(UIUtils.getContext());
            indicators[i].setBackgroundResource(unSelect);
            if (i == 0) {
                indicators[i].setBackgroundResource(select);
            }
            //指示点之间到间距
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(-2, -2);
            params.leftMargin = UIUtils.dip2px(10);
            params.rightMargin = UIUtils.dip2px(10);
            ovels.addView(indicators[i], params);
        }
        return indicators;
    }
}
